package com.nlelpct.manage.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import cn.hutool.core.util.ObjectUtil;

import java.util.HashMap;

/**
 * @author :小马
 * @date : 2023-07-18
 */
public class ThreadParameter {

    private static final ThreadLocal<HashMap<String, String>> LOCAL_MAP = new TransmittableThreadLocal<>();


    public static String getLocalData(String key) {
        HashMap<String, String> threadLocalMap = LOCAL_MAP.get();
        if (ObjectUtil.isEmpty(threadLocalMap)) {
            return "";
        }
        return threadLocalMap.get(key);
    }

    public static void setLocalData(String key, String value) {
        HashMap<String, String> localMap = getLocalMap();
        localMap.put(key, value);
        setLocalMap(localMap);
    }

    public static void setLocalMap(HashMap<String, String> localHashMap) {
        if (ObjectUtil.isEmpty(localHashMap)) {
            localHashMap = new HashMap<>();
        }
        LOCAL_MAP.set(localHashMap);
    }

    public static HashMap<String, String> getLocalMap() {
        HashMap<String, String> threadLocalMap = LOCAL_MAP.get();
        if (ObjectUtil.isEmpty(threadLocalMap)) {
            LOCAL_MAP.set(new HashMap<>());
        }
        return LOCAL_MAP.get();
    }

}
